$(function () {
    $("#post_reply").click(function () {
        editor.focus();
        $('html,body').animate({ scrollTop: $(document).height() }, 500); return false;
    });
    $(".hostre").click(function () {
        editor.focus();
        $('html,body').animate({ scrollTop: $(document).height() }, 500); return false;

    });
    $(".floorre").click(function () {
        var text=$(this).parents(".plhin").find("#postmessage_5897387").html();
        var count=$(this).parents(".plhin").find("#postnum5897387 em").text();
        editor.html("回复"+count+"楼：<br /><h1 style='color:#999999;'>“</h1>"+text+"<h1 style='color:#999999;'>”</h1><p></p>");
        editor.focus();
        $('html,body').animate({ scrollTop: $(document).height() }, 500); return false;

    });
    $("#fastpostsubmit").click(function () {
        var text=editor.html();
        var pid=$(".pid").attr("pid");
        var uid=$(".uid").attr("uid");
        var text=editor.html();
        var date=new Date().Format("yyyy-MM-dd HH:mm:ss");
        if(uid==""){
            alert("请先登录！");
            location.href="login.jsp";
        }else{
            addComment(pid,uid,text,date);
        }

    });
});
var editor;
KindEditor.ready(function(K) {
    editor = K.create('textarea[name="content"]', {
        cssPath : 'css/themes/default/default.css',
        uploadJson : 'upload_json.jsp',
        fileManagerJson : 'file_manager_json.jsp',
        allowFileManager : true,
        width : '720px',
        height : '245px',
        resizeType: '0',
        afterCreate : function() {
            var self = this;
            K.ctrl(document, 13, function() {
                self.sync();
                document.forms['example'].submit();
            });
            K.ctrl(self.edit.doc, 13, function() {
                self.sync();
                document.forms['example'].submit();
            });
        }
    });
    prettyPrint();
});
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function addComment(pid,uid,text,ctime) {
    console.log(text);
    $.ajax({
        type: "POST",
        url: "comment?comop=addcomment",
        data: {"pid": pid,"uid":uid,"content":text,"ctime":ctime},
        async: false,
        success: function (data) {
            console.log(data);
            if(data>0){
                alert("回复成功！");
                location.reload();
            }else{
                alert("回复失败！");
            }
        },
        error: function () {
            console.log("error");
        }
    });
}